package com.gexiaobao.pigeon.ui.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.widget.AppCompatButton
import androidx.appcompat.widget.AppCompatEditText
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.view.isVisible
import androidx.core.widget.addTextChangedListener
import com.gexiaobao.pigeon.R

/**
 * @author:hxw
 * @date  :2022/12/8
 * @desc  :
 */
class RxDialogPageTurning(context: Context?) : RxDialog(context!!) {

    private lateinit var sureView: AppCompatButton
    private lateinit var titleView: AppCompatTextView
    private lateinit var ivClose: AppCompatImageView
    private lateinit var etDialogCurrentPage: AppCompatEditText
    private lateinit var ivDialogPageMinus: AppCompatImageView
    private lateinit var ivDialogPagePlus: AppCompatImageView

    init {
        initView()
    }

    @SuppressLint("MissingInflatedId")
    private fun initView() {
        val dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_page_turning, null)
        sureView = dialogView.findViewById(R.id.btnDialogPageSure)
        titleView = dialogView.findViewById(R.id.tvDialogPageTitle)
        ivClose = dialogView.findViewById(R.id.ivDialogPageClose)
        etDialogCurrentPage = dialogView.findViewById(R.id.tvDialogCurrentPage)
        ivDialogPageMinus = dialogView.findViewById(R.id.ivDialogPageMinus)
        ivDialogPagePlus = dialogView.findViewById(R.id.ivDialogPagePlus)

        ivClose.setOnClickListener {
            dismiss()
        }

        etDialogCurrentPage.addTextChangedListener {
            if (it != null) {
                etDialogCurrentPage.setSelection(it.length)
            }
        }

        setContentView(dialogView)
    }

    fun setMinusListener(listener: View.OnClickListener?) {
        ivDialogPageMinus.setOnClickListener(listener)
        etDialogCurrentPage.text?.let { etDialogCurrentPage.setSelection(it.length) }
    }

    fun setPlusListener(listener: View.OnClickListener?) {
        ivDialogPagePlus.setOnClickListener(listener)
        etDialogCurrentPage.text?.let { etDialogCurrentPage.setSelection(it.length) }
    }

    fun setCloseListener(listener: View.OnClickListener?) {
        ivClose.setOnClickListener(listener)
    }

    fun setSureListener(listener: View.OnClickListener?) {
        sureView.setOnClickListener(listener)
    }

    fun setCurrentPage(pageCurrent: String) {
        etDialogCurrentPage.setText(pageCurrent)
    }

    fun getCurrentPage(): String {
        return etDialogCurrentPage.text.toString().trim()
    }

//    fun setTotalPage(pageTotal: String) {
//        if (pageTotal.isEmpty()) {
//            tvDialogTotalPage.isVisible = false
//            return
//        }
//        tvDialogTotalPage.isVisible = true
//        tvDialogTotalPage.text = "/$pageTotal"
//    }

    fun setTitle(titleStr: String) {
        if (titleStr.isEmpty()) {
            titleView.visibility = View.GONE
            return
        }
        titleView.text = titleStr
        titleView.visibility = View.VISIBLE
    }
}